package inf3791.calendarsystem.others.components;

import inf3791.calendarsystem.shared.Constants;
import inf3791.calendarsystem.shared.DaySpec;
import java.util.Calendar;

/**
 *
 * @author Jonas Weber
 */
public class DateAnnouncer extends javax.swing.JPanel {

    private boolean isSameDay(Calendar a, Calendar b) {
        return (a.get(Calendar.DATE) == b.get(Calendar.DATE)
                && a.get(Calendar.MONTH) == b.get(Calendar.MONTH)
                && a.get(Calendar.YEAR) == b.get(Calendar.YEAR));
    }

    private String getSpecialDescription(DaySpec ds) {
        Calendar given = Calendar.getInstance();
        given.set(Calendar.DATE, ds.day);
        given.set(Calendar.MONTH, ds.month - 1);
        given.set(Calendar.YEAR, ds.year);

        Calendar rightnow = Calendar.getInstance();

        rightnow.add(Calendar.DATE, -1);
        if (isSameDay(given, rightnow)) {
            return "I Går";
        }

        rightnow.add(Calendar.DATE, 1);
        if (isSameDay(given, rightnow)) {
            return "I Dag";
        }

        rightnow.add(Calendar.DATE, 1);
        if (isSameDay(given, rightnow)) {
            return "I Morgen";
        }

        return null;
    }
    private DaySpec ds;

    public DaySpec getDate() {
        return ds;
    }

    public void setDate(DaySpec ds) {
        this.ds = ds;

        if (ds != null) {

            String desc = getSpecialDescription(ds);
            if (desc != null) {
                labelDate.setText(" ");
                labelMonth.setText(desc);
            } else {
                labelDate.setText(String.format("%02d.", ds.day));
                labelMonth.setText(String.format("%s %d", Constants.MONTHNAMES[ds.month - 1], ds.year));
            }
        } else {
            labelDate.setText(" ");
            labelMonth.setText(" ");
        }
    }

    /**
     * Creates new form DateAnnouncer
     */
    public DateAnnouncer() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelDate = new javax.swing.JLabel();
        labelMonth = new javax.swing.JLabel();

        labelDate.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
        labelDate.setForeground(new java.awt.Color(0, 0, 255));
        labelDate.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        labelDate.setText("00.");

        labelMonth.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        labelMonth.setText("None");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelDate, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelMonth, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelMonth)
                    .addComponent(labelDate)))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JLabel labelDate;
    javax.swing.JLabel labelMonth;
    // End of variables declaration//GEN-END:variables
}
